package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.Reserve;
import com.itheima.service.OrderService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 根据预约id查询预约信息
     * @param id
     * @return
     */
    public Order findById(String id) {
        Order order = orderMapper.findById(id);
        return order;
    }

    /**
     * 体检预约
     * @param reserve
     * @return
     */
    public Result submitOrder(Reserve reserve) throws Exception{
        // 1. 预约日期未设置预约人数，无法预约
        Date orderDate = reserve.getOrderDate();
        // 将Date转换为LocalDate
        LocalDate localdate = orderDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        // 将 LocalDate 转换回 Date
        orderDate = Date.from(localdate.atStartOfDay(ZoneId.systemDefault()).toInstant());
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(orderDate);

        if (orderSetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        // 2、预约日期如果已满，则不能预约
        int number = orderSetting.getNumber(); // 可预约人数
        int reserveNum = orderSetting.getReservations(); // 已预约人数

        if (reserveNum >= number){
            return new Result(false,MessageConstant.ORDER_FULL);
        }

        // 3、不能重复预约
        String telephone = reserve.getTelephone();
        Member member = memberMapper.findByTelephone(telephone);
        if(member != null){
            Integer memberId = member.getId();
            int setmealId = reserve.getSetmealId();
            Order order = new Order(memberId,orderDate,null,null,setmealId);
            List<Order> list = orderMapper.findByCondition(order);
            if(list != null && list.size() > 0){
                //已经完成了预约，不能重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        //4、当前用户不是会员，需要自动完成注册
        if (member == null){
            member = new Member();
            member.setName(reserve.getName());
            member.setPhoneNumber(telephone);
            member.setIdCard(reserve.getIdCard());
            member.setSex(reserve.getSex());
            member.setRegTime(new Date());
            memberMapper.add(member);
        }

        //5、将信息保存到订单表
        Order order = new Order(member.getId(),
                orderDate,
                reserve.getOrderType(),
                Order.ORDERSTATUS_NO,
                reserve.getSetmealId());
        orderMapper.add(order);

        //6、预约成功，更新当日的已预约人数
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingMapper.editReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());

    }
}
